{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OOP or object oriented programming\n", "When you program in Python you are programming with objects\n", " - Everything in Python is an object (excluding certain statements like 'for', 'def', etc)\n", " - An object is effectively a self contained enviornment\n", " - An object has local variables (attributes)\n", " - An object has localized functions (methods)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(isinstance(1, object)) #the isinstance() function tells you what type the object is\n", "print(isinstance(1, str)) # should return False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# What does it mean to have attributes and methods?\n", "\n", "lst = [1,2,3,4]\n", "print(dir(lst)) # dir() function lets us see what attributes/methods exist\n", "print()\n", "print(f\"the lists __len__() is - {lst.__len__()}\") # Any value returned from dir() can be called from the object\n", "# using () at the end means it's a method (function call), vs an attribute (variable)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### In class work" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", "Use dir on a list, dict, string\n", "Look at some of the methods we've already used (append, extend, keys, etc)\n", "See if you can get some of these new methods working (Ex. floats is_integer)\n", "\"\"\"\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating objects\n", "Objects unlike functions are defined by **class**. That keyword is followed by the class name, and capped off by **():**.\n", "\n", "Class name formatting:\n", " - CamelCasing\n", " - Informative name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#What does an object look like?\n", "\n", "class FirstObj():\n", " class_attr = \"constant value\"\n", " \n", " def __init__(self, val): # The init method is called every time we initalize an object\n", " self.inst_attr = \"variable specific to our instance\" # set attribute using self.'attr'\n", " self.val = val\n", " \n", " def increment(self, a): # An instance method (always takes self), used to perform action on the instance\n", " self.val += a # We can update instance variables by accessing them using self\n", " \n", " def __repr__(self): # __repr__ is how our object is represented in the console\n", " return(f'val: {self.val}')\n", " \n", "obj1 = FirstObj(5) # Here we create a FirstObj instance using 5 for our val\n", "print(f\"inst_attr = {obj1.inst_attr}, val = {obj1.val}\") #We can call any attribute through the instance\n", "obj1.increment(3) # We can also call instance methods (they take self by default)\n", "print(f\"val = {obj1.val}\")\n", "\n", "print(obj1) # This shows us what the __repr__ method does\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Instance vs Class attributes\n", "An instance attribute is an attribute associated with a specific instantiation of a class or \"object-level variable\". This means that the value is tied only to that instance of the class.\n", "\n", "A class attribute is an attribute associated with the class, meaning that the value propogates through all objects." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "class FirstObj():\n", " class_attr = \"Class level attribute\" #Class level attributes are constant across all Person objects\n", " \n", " def __init__(self): #Every object method takes self as first parameter, self is the intance\n", " self.inst_attr = \"Inst level attribute\"\n", " \n", "obj1 = FirstObj()\n", "obj2 = FirstObj()\n", "print(\"\\n********Class variables:\")\n", "print(f\"obj1 - {obj1.class_attr}\")\n", "print(f\"obj2 - {obj2.class_attr}\")\n", "\n", "print(\"\\n********Changing Class variables:\")\n", "FirstObj.class_attr = \"These changes cascade throughout all objects\"\n", "print(f\"obj1 - {obj1.class_attr}\")\n", "print(f\"obj2 - {obj2.class_attr}\")\n", "\n", "print(\"\\n********Changing Instance variables:\")\n", "obj1.inst_attr = \"This only changes obj1\"\n", "print(f\"obj1 - {obj1.inst_attr}\")\n", "print(f\"obj2 - {obj2.inst_attr}\")\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### In class work" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Problem 1\n", "\"\"\"\n", "Create a class 'Person' with the following\n", " - attributes height(inches), weight, name, age\n", " - A method inFeet() that prints out their height in feet and inches (E.X. 62\" -> 5' and 2\")\n", " - A method birthday() that increases the age by 1\n", " - A method '__repr__(self)' that returns a string containing all 3 attributes\n", " \n", "Once done call print('instance of a Person')\n", "\"\"\"\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }